package com.unism.common.threadPool;

import java.util.HashMap;
import java.util.Map;

public class TreadPoolFactory {

	
	private static Map<String, ThreadPool.Pool> mapPools =  new HashMap<String, ThreadPool.Pool>();
	
	/**
	 * 初始化线程池
	 * @param threadPoolName 线程池和线程名称
	 * @param maxThreadNum 线程池最大线程数
	 * @param minThreadNum 线程池最小线程数，或初始线程数
	 * @param freeTimeout 空闲线程超时时长(秒)
	 * @param busyTimeout 忙碌线程超时时长(秒)
	 * @return 线程池实例
	 */
	public final static void initThreadPool(String poolName, 
			int maxNum,
			int minNum, 
			long freeTimeout, 
			long busyTimeout) throws Exception {
		ThreadPool.Pool pool = mapPools.get(poolName);
		if(pool!= null){
			throw new Exception("线程池不能重复初始化！");
		}
		if(pool == null){
			pool =new ThreadPoolImp.MyThreadPool(poolName, maxNum, minNum, freeTimeout, busyTimeout);
			mapPools.put(poolName, pool);
		}
	}
	/**
	 * 初始化线程池
	 * @param poolName线程池和线程名称
	 * @throws Exception
	 */
	public final static void initThreadPool(String poolName) throws Exception {
		ThreadPool.Pool pool = mapPools.get(poolName);
		if(pool!= null){
			throw new Exception("线程池不能重复初始化！");
		}
		if(pool == null){
			pool = new ThreadPoolImp.MyThreadPool(poolName,null,null,null,null);
			mapPools.put(poolName, pool);
		}
	}
	/**
	 * 得到唯一线程池实例
	 * @param dataSourceName
	 * @return
	 * @throws Exception
	 */
	public final static ThreadPool.Pool getThreadPool(String poolName)
		throws Exception {
		ThreadPool.Pool pool = mapPools.get(poolName);
		if (pool == null) {
			throw new Exception("得到线程池前首先必须初始化！");
		}
		return pool ;
	}

}
